-- SkyWhirlpoolM
-- created by huanghr
-- 时空裂痕

module("SkyWhirlpoolM", package.seeall);

local skyWhirlPoolCfg = {};
local campCambatMap = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    skyWhirlPoolCfg = {};
    campCambatMap   = {};

    -- 转换下sky_whirlpool_CSV信息表
    local sky_whirlpool_CSV = get_sky_whirlpool_CSV();
    for i = 1, #sky_whirlpool_CSV do
        local row = sky_whirlpool_CSV[i];

        skyWhirlPoolCfg[row.id] = row;

        if campCambatMap[row.camp] == nil then
            campCambatMap[row.camp] = {};
        end

        if campCambatMap[row.camp][row.combat_num] == nil then
            campCambatMap[row.camp][row.combat_num] = {};
        end

        campCambatMap[row.camp][row.combat_num][row.index] = row.id;
    end
end

-- 模块初始化操作
function init()
    loadCsv();

    EventMgr.removeAll("SkyWhirlpoolM");
end

-- 检索
function query(id, path)
    local m = skyWhirlPoolCfg[id];

    if not m or not path then
        return m;
    end

    return m[path];
end

-- 获得当前参战id
function queryDefenserId(camp, combatNum, index)
    return campCambatMap[camp][combatNum][index];
end

-- 获得当前防守方的信息
function getDefenserInfo(id, exploreLevel, whpCombat)

    local data = query(id);

    --  战斗相关的属性
    local  army = data["army"];
    local combat_dbase = data["combat_dbase"];
    local ability_fix = combat_dbase["ability_fix"] or 0;
    local fire = combat_dbase["fire"];
    local armor = combat_dbase["armor"];
    local speed = combat_dbase["speed"];
    local luck = combat_dbase["luck"];

    -- TODO: 需要根据探索等级计算各种属性
    return table.add({
        ["fight_ability"] = FormulaM.invoke("CALC_WHIRLPOOL_FIGHT_ABILITY", exploreLevel, ability_fix),
        ["name"]          = data["name"],
        ["fire"]          = FormulaM.invoke("CALC_WHIRLPOOL_INIT_ATTRIB", exploreLevel, fire),
        ["armor"]         = FormulaM.invoke("CALC_WHIRLPOOL_INIT_ATTRIB", exploreLevel, armor),
        ["speed"]         = FormulaM.invoke("CALC_WHIRLPOOL_INIT_ATTRIB", exploreLevel, speed),
        ["luck"]          = FormulaM.invoke("CALC_WHIRLPOOL_INIT_ATTRIB", exploreLevel, luck),
        ["pets"]          = army,
        ["ship_skills"]   = data["skills"],
        ["whirlpool_combat"] = whpCombat;
        ["explore_level"] = exploreLevel;

    }, data["dbase"]);
end

-- 计算对应四维和战力
function calcWhirlpoolAttrib(id, exploreLevel)
    local data = query(id);

    --  战斗相关的属性
    local combat_dbase = data["combat_dbase"];
    local ability_fix = combat_dbase["ability_fix"] or 0;
    local fire = combat_dbase["fire"];
    local armor = combat_dbase["armor"];
    local speed = combat_dbase["speed"];
    local luck = combat_dbase["luck"];

    -- TODO: 需要根据探索等级计算各种属性
    return {
        ["fight_ability"] = FormulaM.invoke("CALC_WHIRLPOOL_FIGHT_ABILITY", exploreLevel, ability_fix),
        ["fire"]          = FormulaM.invoke("CALC_WHIRLPOOL_INIT_ATTRIB", exploreLevel, fire),
        ["armor"]         = FormulaM.invoke("CALC_WHIRLPOOL_INIT_ATTRIB", exploreLevel, armor),
        ["speed"]         = FormulaM.invoke("CALC_WHIRLPOOL_INIT_ATTRIB", exploreLevel, speed),
        ["luck"]          = FormulaM.invoke("CALC_WHIRLPOOL_INIT_ATTRIB", exploreLevel, luck),
    }
end